package com.h3c.gm.service

import com.google.common.collect.Lists
import com.google.common.collect.Maps
import com.h3c.gm.common.utils.ListUtils
import com.h3c.gm.dao.IBusHostSpecDao
import com.h3c.gm.dao.IBusOrgDao
import com.h3c.gm.entity.DO.BusHostSpecDO
import com.h3c.gm.entity.VO.BusHostSpecVO
import org.apache.commons.lang3.tuple.Pair
import org.springframework.stereotype.Service

import javax.annotation.Resource

/**
 * Created by d14287 on 2017/6/26.
 */
@Service
class BusHostSpecService {
    @Resource
    IBusHostSpecDao busHostSpecDao
    @Resource
    IBusOrgDao busOrgDao

    Map<String, Object> getHostsStatistics() {
        def map = Maps.newHashMap()
        map.put('count', busHostSpecDao.getCount() == null ? 0 : busHostSpecDao.getCount(Maps.newHashMap()))
        map.put('sumcpus', busHostSpecDao.getSumCpus() == null ? 0 : busHostSpecDao.getSumCpus(Maps.newHashMap()))
        map.put('sumram', busHostSpecDao.getSumRam() == null ? 0 : busHostSpecDao.getSumRam(Maps.newHashMap()))
        return map
    }

    int getCountByOrgkey(String orgkey) {
        def queryParams = Maps.newHashMap()
        def orgkeyList = Lists.newArrayList()
        orgkeyList.add(orgkey)
        queryParams.put('orgkeyList', orgkeyList)
        busHostSpecDao.getCount(queryParams)
    }

    Map<String, Object> getProHostsStatistics(String province) {
        def orgkeyList = getOrgkeyList(province)
        def queryParams = Maps.newHashMap()
        queryParams.put('orgkeyList', orgkeyList)
        def map = Maps.newHashMap()
        map.put('count', busHostSpecDao.getCount() == null ? 0 : busHostSpecDao.getCount(queryParams))
        map.put('sumcpus', busHostSpecDao.getSumCpus() == null ? 0 : busHostSpecDao.getSumCpus(queryParams))
        map.put('sumram', busHostSpecDao.getSumRam() == null ? 0 : busHostSpecDao.getSumRam(queryParams))
        return map
    }

    Map<String, Object> getHostsStatusStatistics(String orgkey) {
        def resultMap = Maps.newHashMap()
        def paramMap = Maps.newHashMap()
        def activeMap = Maps.newHashMap()
        def errorMap = Maps.newHashMap()
        def shutoffMap = Maps.newHashMap()
        def recycleMap = Maps.newHashMap()

        paramMap.put('orgkey', orgkey)
        def statusList = Lists.newArrayList()
        paramMap.put('statusList', statusList)

        int activeNum, errorNum, shutoffNum, recycleNum
        //正常
        statusList.add('ACTIVE')
        activeMap.put('active', activeNum = busHostSpecDao.getCountByStatus(paramMap))
        //异常
        statusList.clear()
        statusList.add('ERROR')
        errorMap.put('error', errorNum = busHostSpecDao.getCountByStatus(paramMap))
        //关闭 STOPPED SHUTDOWN SHUTOFF
        statusList.clear()
        statusList.add('STOPPED')
        statusList.add('SHUTDOWN')
        statusList.add('SHUTOFF')
        shutoffMap.put('shutoff', shutoffNum = busHostSpecDao.getCountByStatus(paramMap))
        //回收站
        statusList.clear()
        statusList.add('RECYCLE')
        recycleMap.put('recycle', recycleNum = busHostSpecDao.getCountByStatus(paramMap))

        def reStatusList = Lists.newArrayList()
        reStatusList.add(activeMap)
        reStatusList.add(errorMap)
        reStatusList.add(shutoffMap)
        reStatusList.add(recycleMap)
        resultMap.put('statusStatistics', reStatusList)

        resultMap.put('count', activeNum + errorNum + shutoffNum + recycleNum)
        return resultMap
    }

    List<Map<String, Object>> getMonthStatistics(Date from, Date to) {
        def queryParams = Maps.newHashMap()
        queryParams.put('from', from)
        queryParams.put('to', to)
        busHostSpecDao.getMonthStatistics(queryParams)
    }

    List<Map<String, Object>> getProMonthStatistics(Date from, Date to, String province) {
        def orgkeyList = getOrgkeyList(province)
        def queryParams = Maps.newHashMap()
        queryParams.put('from', from)
        queryParams.put('to', to)
        queryParams.put('orgkeyList', orgkeyList)
        busHostSpecDao.getMonthStatistics(queryParams)
    }


    void insert(BusHostSpecDO busHostSpecDO) {
        busHostSpecDao.insert(busHostSpecDO)
    }

    List<String> getOrgkeyList(String province) {
        def busOrgDOs = busOrgDao.getCityOrg(province)
        def orgkeys = Lists.newArrayList()
        busOrgDOs.each {
            item ->
                orgkeys.add(item.getOrgkey())
        }
        return orgkeys
    }

    void deleteByOrgkey(String orgkey) {
        busHostSpecDao.deleteByOrgkey(orgkey)
    }

}
